package com.ruoyi.common.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
//import com.ruoyi.common.config.PortalConfig;
import com.ruoyi.common.config.PortalConfig;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * http工具类
 *
 * @author 徐岩松
 * @date 2023年07月28日
 */
@Slf4j
public class HttpUtils {

    public static final String GET = "get";
    public static final String POST = "post";
    public static final String DELETE = "delete";
    public static final String PUT = "put";
    public static final int CONNECT_TIMEOUT = 60;
    public static final int READ_TIMEOUT = 60;
    public static final int WRITE_TIMEOUT = 30;


    /**
     * 创建header
     *
     * @param params 参数个数
     * @return {@link Map}<{@link String}, {@link String}>
     */
    public static Map<String, String> createHeader(Map<String, Object> params) {

        String appId = SpringUtils.getBean(PortalConfig.class).getAppId();
        String privateKey = SpringUtils.getBean(PortalConfig.class).getPrimaryKey();
        Map<String, String> header = new HashMap<>();
        String timeStamp = Convert.toStr(DateUtil.current());
        header.put("appId", appId);
        header.put("timeStamp", timeStamp);
        params.put("appId", appId);
        params.put("timeStamp", timeStamp);
        String paramsStr = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
        log.info("最终签名参数: {}", paramsStr);
        String sign = Sm2Utils.sign(privateKey, paramsStr);
        header.put("signature", sign);
        return header;
    }

    /**
     * get 方法
     *
     * @param url       url
     * @param reqParams 要求参数
     * @return {@link String}
     */
    public static String doGet(String url, Map<String, Object> reqParams) {
        return doGet(url, reqParams, null);
    }

    public static String doGet(String url, Map<String, Object> reqParams, Map<String, String> head) {
        return commonHead(HttpRequest.get(url), head)
                .form(reqParams)
                .execute().body();
    }

    /**
     * post 方法
     *
     * @param url       url
     * @param reqParams 要求参数
     * @return {@link String}
     */
    public static String doPost(String url, Map<String, Object> reqParams) {
        return doPost(url, reqParams, null);
    }

    public static String doPost(String url, String reqParams) {
        return doPost(url, reqParams, null);
    }

    public static String doPost(String url, Map<String, Object> reqParams, Map<String, String> head) {
        return commonHead(HttpRequest.post(url), head)
                .form(reqParams)
                .execute().body();
    }

    public static String doPost(String url, String reqParams, Map<String, String> head) {
        return commonHead(HttpRequest.post(url), head)
                .body(reqParams)
                .execute().body();
    }

    public static String doDelete(String url, Map<String, Object> reqParams, Map<String, String> head) {
        return commonHead(HttpRequest.delete(url), head)
                .form(reqParams)
                .execute().body();
    }

    public static String doDelete(String url, String reqParams, Map<String, String> head) {
        return commonHead(HttpRequest.delete(url), head)
                .body(reqParams)
                .execute().body();
    }

    public static String doPut(String url, String reqParams, Map<String, String> head) {
        return commonHead(HttpRequest.put(url), head)
                .body(reqParams)
                .execute().body();
    }

    /**
     * 方法commonHead功能描述:TODO 构建请求公用头部
     *
     * @Author Turbo
     * @date 2022/2/21 9:45 AM
     */
    private static HttpRequest commonHead(HttpRequest hq, Map<String, String> head) {
        hq.header("Content-Type", "application/json;charset=UTF-8");
        if (head != null) {
            for (Map.Entry<String, String> entry : head.entrySet()) {
                hq.header(entry.getKey(), entry.getValue());
            }
        }
        log.info("commonHead: {}", hq);
        return hq;
    }

}
